package com.choosefine.springboot.devops.activityImg.controller;


import com.choosefine.springboot.devops.activity.dto.InsertActivityDTO;
import com.choosefine.springboot.devops.activityImg.dto.InsertActivityImgDTO;
import com.choosefine.springboot.devops.activityImg.dto.UpdateActivityImgDTO;
import com.choosefine.springboot.devops.activityImg.model.ActivityImg;
import com.choosefine.springboot.devops.activityImg.service.ActivityImgService;
import com.choosefine.springboot.devops.base.config.FileUtil;
import com.choosefine.springboot.devops.base.config.MapUtil;
import com.choosefine.springboot.devops.base.result.ReturnMSG;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/activityImg")
public class ActivityImgController {
	@Autowired
	private ActivityImgService activityImgService;
	@Autowired
	private ObjectMapper objectMapper;
	private final String filePath = "d:/roncoo/ttt/";

	@GetMapping("/{id}")
	public String selectById(@PathVariable("id") Integer id) throws JsonProcessingException {
		ActivityImg result=activityImgService.selectById(id);
		return objectMapper.writeValueAsString(result);
	}

	@PostMapping
	public String save(@Valid InsertActivityImgDTO insertActivityImgDTO, BindingResult result) throws JsonProcessingException {
		ReturnMSG returnMSG=new ReturnMSG();
		if(result.hasErrors()){
			return objectMapper.writeValueAsString(returnMSG.paramError());
		}else{
			List<String> urlList=new ArrayList<String>();
			for(MultipartFile now:insertActivityImgDTO.getRoncooFile()){
				if(!FileUtil.uploadsUtil(filePath,now,urlList)){
					//代表上传错误。可以考虑是否删掉以上传成功的图片。
					FileUtil.deleteUtil(filePath,urlList);
					return objectMapper.writeValueAsString(returnMSG.uploadError());
				}
			}
			for(String now:urlList){
				ActivityImg activityImg=new ActivityImg();
				BeanUtils.copyProperties(insertActivityImgDTO,activityImg);
				activityImg.setUrl(now);
				activityImgService.save(ActivityImg.class, MapUtil.transBean2Map(activityImg));
			}
			return objectMapper.writeValueAsString(returnMSG.success());
		}
	}

	@PutMapping
	public String update(@Valid UpdateActivityImgDTO updateActivityImgDTO, BindingResult result) throws JsonProcessingException {
		ReturnMSG returnMSG=new ReturnMSG();
		if(result.hasErrors()){
			return objectMapper.writeValueAsString(returnMSG.paramError());
		}else{
			if(activityImgService.update(ActivityImg.class, MapUtil.transBean2Map(updateActivityImgDTO))>0){
				return objectMapper.writeValueAsString(returnMSG.success());
			}else{
				return objectMapper.writeValueAsString(returnMSG.error());
			}
		}
	}

	@DeleteMapping
	public String delete(Integer id) throws JsonProcessingException {
		ReturnMSG returnMSG=new ReturnMSG();
		if(id==null){
			return objectMapper.writeValueAsString(returnMSG.paramError());
		}else{
			Map param=new HashMap();
			param.put("id",id);
			if(activityImgService.falseDelete(ActivityImg.class,param)>0){
				return objectMapper.writeValueAsString(returnMSG.success());
			}else{
				return objectMapper.writeValueAsString(returnMSG.error());
			}
		}
	}
}
